/*
 * 创建日期 2005-10-9
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.dp;

import java.math.BigDecimal;
import java.util.List;

import com.vanda.report.ReportException;
import com.vanda.report.ReportGroupRow;
import com.vanda.report.ReportUtils;
import com.vanda.report.Validation;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class MaxElement extends AggregationElement {
	/* 
	 * @see com.vanda.report.element.Element#validator()
	 */
	public void validator() throws ReportException {
		Validation.checkRequired("MaxElement","field",super.getField());

	}
	
	/* （非 Javadoc）
	 * @see com.vanda.report.dp.AggregationElement#aggregate(java.util.List, java.lang.String)
	 */
	public Object[] aggregate(List beanList,int start,int end, String[] property)
		throws ReportException {
		Object[] objs = new Object[property.length];
	
		try{
			BigDecimal[] max = new  BigDecimal[property.length];
			for(int i=0;i<max.length;i++){
				max[i] = new BigDecimal(-Double.MAX_VALUE);
			}
			
			Object[] dataObj = new Object[property.length];
			for(int i=start;i<end;i++){
				Object temp = beanList.get(i);
				
				if(temp instanceof ReportGroupRow){
					continue;
				}
				for(int j=0;j<property.length;j++){
					Object value = super.getProperty(temp,property[j]);
					if(dataObj[j] == null){
						dataObj[j] = value;
					}
					BigDecimal bg = ReportUtils.convertBg(value);
				
					if(bg != null){
						if(max[j].compareTo(bg) < 0){
							max[j] = bg;
						}
					}
				}
			}
			
			for(int i=0;i<objs.length;i++){
				objs[i] = ReportUtils.convertObj(max[i],dataObj[i]);
			}
		}catch(Exception e){
			throw new ReportException(e);
		}

	
		return objs;
	}

}
